package com.alibaba.algo.test;

import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * @Description: 签名计算
 * @author: malin
 * @date: 2020-12-16 14:59:35
 */
public class SignCalculateTest {
	public static void main(String[] args) {
		// 请求接口POST url = "http://127.0.0.1/test/demo";
		// POSThttp://127.0.0.1/test/demo
		Map<String, String> params = new TreeMap<String, String>();
		params.put("name", "admin");
		params.put("passwd", "abc123");
		params.put("token",
				"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzbWFydEFwcCIsImF1ZCI6IjE2MDgxMDA1NjA3MDMiLCJleHAiOjE2MDg1MzI1NjB9.w3prw41Z7W3se7aow61xCZft5FCHLY2USV-4T-RChnc");
		params.put("timestamp", "1608105473502");
		params.put("appid", "10010");
		params.put("nonce", "1246677A1");

		StringBuilder buf = new StringBuilder();
		buf.append("POST").append("http://127.0.0.1/test/demo");
		for (Entry<String, String> param : params.entrySet()) {
			buf.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		String appSecret = "12345675abcx";
		buf.append(appSecret);
		System.err.println("buf=" + buf.toString());
		//POSThttp://127.0.0.1/test/demoappid=10010&name=admin&nonce=1246677A1&passwd=abc123&timestamp=1608105473502&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzbWFydEFwcCIsImF1ZCI6IjE2MDgxMDA1NjA3MDMiLCJleHAiOjE2MDg1MzI1NjB9.w3prw41Z7W3se7aow61xCZft5FCHLY2USV-4T-RChnc&12345675abcx
		System.err.println("sign=" + DigestUtils.md5Hex(buf.toString()));
		
		/**
		 * ------------------------------------------------------
		 * 接口URL:http://127.0.0.1/test/demo
		 * 请求方式：POST
		 * 请求参数：JSON {"name":"admin","passwd":"abc123"}
		 * 请求head:{token,appid,nonce,timestamp}
		 * ------------------------------------------------------
		 * 签名计算步骤
			1.	将请求方法+请求地址+所有参数按照参数名的字母顺序升序排序后拼接+appSecret
			2.	将第1步的结果MD5后生成签名参数sign
	
			说明
			1.	请求方法为GET或POST，注意大写。
			2.	请求地址为客户端请求完整地址，如：http://127.0.0.1/test/demo
			3.	参数拼接，按照参数名的 【字母顺序升序】 排序后进行拼接，如：appid=xxx&name=xxx&nonce=value1&passwd=value2&
			4.	组成完整的拼接，如：POSThttp://127.0.0.1/test/demoappid=10010&name=admin&nonce=1246677A1&passwd=abc123&timestamp=1608105473502&token=eyJ0eXAiO&appSecret
			5.	生成MD5值，如：A1CD67A29ADD450EF067517D47FE4E51
		 */
	}
}
